package com.yc.baseapi;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class TestMap {
	
	public void testMap() {
		Map<String, String> m = new HashMap<String, String>();
//		var map = new HashMap<String, Integer>();//jdk10以后
//		m.merge(key, value, remappingFunction)
	}
	
	/**
	 * merge方法（sine jdk8）
	 * merge英文解释：合并
	 * 作用：参数1：key   参数2：一个值     参数3：函数
	 * 当key不存在把值赋值给这个key的值
	 * 当key存在，把第二个值和原值做一个函数处理
	 */
	public void testMerge() {
		Map<String, Integer> mm = new HashMap<String, Integer>();
		mm.put("aa", 1);
		mm.merge("aa", 2, (oldv, newv) -> oldv + newv);
//		mm.merge("bb", 2, new remappingFunction() {
//			
//		});
		System.out.println(mm);
	}
	
	public void testConcurrentHashMap() {
		ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<String, Class>();
//		generatorStrategyToClassNameMap.put(key, value)
	}
	
	public void testMap2() {
		Map<String, Integer> mm = new HashMap<String, Integer>();
		mm.put("aa", 1);
		
		for (String str : mm.keySet()) {
			
		}
	}
	
	//报错：java.util.ConcurrentModificationException
	public void testRemoved() {
		Map<Integer, String> m = new HashMap<Integer, String>();
		m.put(1, "aa");
		m.put(2, "bb");
		m.put(3, "cc");
		
		for(Integer key : m.keySet()) {
			if (2 == key) {
				m.remove(key);
			}
		}
		
	}
	
	//用迭代器删除
	public void testRemoved2() {
		Map<Integer, String> m = new HashMap<Integer, String>();
		m.put(1, "aa");
		m.put(2, "bb");
		m.put(3, "cc");
		
		Iterator<Entry<Integer, String>> iterator = m.entrySet().iterator();
		while(iterator.hasNext()) {
			if(iterator.next().getKey() == 2) {
				iterator.remove();
			}
		}
		
	}
	
	public void m2() {
		List<Integer> a = new ArrayList<>();
		a.add(1);
		a.add(2);
		a.add(3);
		a.add(3333);

		Integer b = 2;
		Integer c = 3333;
		int d = 2;
		int e = 3333;

		System.out.println(a.contains(b));
		System.out.println(a.contains(c));
		System.out.println(a.contains(d));
		System.out.println(a.contains(e));

	}
	

	public static void main(String[] args) {
		TestMap tm = new TestMap();
		tm.m2();
//		tm.testMerge();
//		tm.testRemoved();
//		tm.testRemoved2();

	}

}
